package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;




    @Override
    public AgreementDetailVo getDetailVoById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        LeaseAgreement leaseAgreement = this.getById(id);
        if (ObjectUtils.isEmpty(leaseAgreement)){
            return null;
        }
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        //插入公寓名称
        Long apartmentId = leaseAgreement.getApartmentId();
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper.select(ApartmentInfo::getName).eq(ApartmentInfo::getId, apartmentId);
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(apartmentInfoLambdaQueryWrapper);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        // 插入公寓图片列表
        List<GraphVo> graphVoListByApartmentId = apartmentInfoService.getGraphVoListByApartmentId(apartmentId);
        agreementDetailVo.setApartmentGraphVoList(graphVoListByApartmentId);
        // 插入房间号
        Long roomId = leaseAgreement.getRoomId();
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.select(RoomInfo::getRoomNumber).eq(RoomInfo::getId, roomId);
        RoomInfo roomInfo = roomInfoMapper.selectOne(roomInfoLambdaQueryWrapper);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        // 插入房间图片列表
        List<GraphVo> graphVoListByRoomId = roomInfoService.getGraphVoListByRoomId(roomId);
        agreementDetailVo.setRoomGraphVoList(graphVoListByRoomId);
        // 插入支付方式名称 通过租约表的payment_type_id来实现
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        PaymentType paymentType = paymentTypeMapper.selectById(paymentTypeId);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        // 插入租期月数和租期单位 通过lease_term_id来查询
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseTermId);
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        return agreementDetailVo;
    }

    @Override
    public List<AgreementItemVo> getAgreementItemVoListByPhone(String phone) {
        return leaseAgreementMapper.listItemVoByPhone(phone);
    }
}




